//
//  AViewController.swift
//  MLVisionSDK
//
//  Created by Lemon on 2025/5/15.
//

import UIKit
public class AViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    let titles = ["加载xibACell","加载xibBCell","加载xibView","xibVC","ocVC","bundle图片","bundle文件","三方静态库","SparkChain","opus","iflyAiui"]
    
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        MLResourceLoader.registerCellXib(for: tableView, cellClass: ATableViewCell.self)
        MLResourceLoader.registerCellXib(for: tableView, cellClass: BTableViewCell.self)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return tableView
    }()
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        tableView.reloadData()
//        let bundle = Bundle(for: self.classForCoder)
//        
//        
//        guard let bundleURL = bundle.url(forResource: "MLResources", withExtension: "bundle"),
//              let resourceBundle = Bundle(url: bundleURL) else {
//            fatalError("MLVisionSDKResources.bundle not found!")
//        }
//        let image = UIImage(named: "logo", in: resourceBundle, compatibleWith: nil)
//        let v = UIImageView()
//        v.image = image
//        v.backgroundColor = .red
//        view.addSubview(v)
//        v.snp.makeConstraints { make in
//            make.center.equalToSuperview()
//            make.width.height.equalTo(100)
//        }
//        
//        let b = BViewController()
//        b.view.backgroundColor = .black
//        
//        DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: DispatchWorkItem(block: {
//            self.present(b, animated: true)
//        }))
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        titles.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let title = titles[indexPath.row]
        if indexPath.row == 1 {
            let cell:BTableViewCell = MLResourceLoader.dequeueCell(from: tableView, for: indexPath)
            cell.textLabel?.text = title
            return cell
        }else{
            let cell:ATableViewCell = MLResourceLoader.dequeueCell(from: tableView, for: indexPath)
            cell.myLabel.text = title
            return cell
        }
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0,1:
            break
        case 2:
            let v:AView = MLResourceLoader.loadView(fromXib: "AView")
            v.myLabel.text = "我是来自xib加载的view"
            v.backgroundColor = .lightGray
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(removeSelf(sender:))))
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.width.height.equalTo(200)
            }
            break
        case 3:
            let vc:CViewViewController = MLResourceLoader.loadViewController(CViewViewController.self)
            present(vc, animated: true)
            break
        case 4:
            let vc:BViewController = BViewController()
            present(vc, animated: true)
            break
            
        case 5:
            
            let image = MLResourceLoader.loadImage(named: "logo")
            let v = UIImageView()
            v.image = image
            v.isUserInteractionEnabled = true
            v.backgroundColor = .red
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(removeSelf(sender:))))
            view.addSubview(v)
            v.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.width.height.equalTo(100)
            }
        case 6:
            guard let url = MLResourceLoader.getBundleFile(name: "aiui", type: "cfg")else{
                print(1234)
                return
            }
            
            let content = try! String(contentsOf: url, encoding: .utf8)
            print(content)
            
        case 7:
            
            IFlySpeechUtility.createUtility("4123124213")
            
            break
        case 8:
            let aaa = SparkChain.getInst()
            print(aaa)
            
            break
        case 9:
            let bbb = OpusKit.shared
            print(bbb)
            
            break
        case 10:
            print(IFlySetting.getVersion())
            
            break
        default:
            break
        }
    }
    
    @objc func removeSelf(sender:UITapGestureRecognizer){
        sender.view?.removeFromSuperview()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}



//// 获取 Framework 的 Bundle
//let frameworkBundle = Bundle(for: MLVisionSDK.self)
//print(frameworkBundle.bundlePath) // 输出路径确认
//
//// 加载资源 Bundle
//guard let bundleURL = frameworkBundle.url(forResource: "MLVisionSDKResources", withExtension: "bundle"),
//      let resourceBundle = Bundle(url: bundleURL) else {
//    fatalError("MLVisionSDKResources.bundle not found!")
//}
//
//// 使用资源
//let image = UIImage(named: "icon", in: resourceBundle, compatibleWith: nil)
